package com.changgou.goods.dao;

import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface SkuMapper extends Mapper<Sku> {

    //扣减库存并增加销量
    @Update("update tb_sku set num=num-#{num},sale_num=sale_num+#{num} where id=#{skuId} and num>=#{num}")
    int decrCount(OrderItem orderItem);

    //增加库存扣减销量
    @Update("update tb_sku set num=num+#{num},sale_num=sale_num-#{num} where id=#{skuId}")
    int resumeStockNum(@Param("skuId") String skuId, @Param("num") Integer num);


    //查询热门商品前十
    @Select("select * from tb_sku order by sale_num desc limit 0,10")
    @Results({@Result(column = "alert_num", property = "alertNum"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime"),
            @Result(column = "spu_id", property = "spuId"),
            @Result(column = "category_id", property = "categoryId"),
            @Result(column = "category_name", property = "categoryName"),
            @Result(column = "brand_name", property = "brandName"),
            @Result(column = "sale_num", property = "saleNum"),
            @Result(column = "comment_num", property = "commentNum")})
    List<Sku> findPopular();
}
